package org.example.backend.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.example.backend.dto.AnimalDTO;
import org.example.backend.dto.Response;
import org.example.backend.entity.Animal;
import org.example.backend.service.AnimalService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Tag(name = "动物管理", description = "动物信息管理相关接口")
@RestController
@RequestMapping("/animals")
@RequiredArgsConstructor
public class AnimalController {

    private final AnimalService animalService;

    @Operation(summary = "创建动物")
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public Response<Animal> createAnimal(@Valid @RequestBody AnimalDTO animalDTO) {
        return Response.success(animalService.createAnimal(animalDTO));
    }

    @Operation(summary = "更新动物信息")
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Response<Animal> updateAnimal(@PathVariable Long id, @Valid @RequestBody AnimalDTO animalDTO) {
        return Response.success(animalService.updateAnimal(id, animalDTO));
    }

    @Operation(summary = "删除动物")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Response<Void> deleteAnimal(@PathVariable Long id) {
        animalService.deleteAnimal(id);
        return Response.success();
    }

    @Operation(summary = "获取动物详情")
    @GetMapping("/{id}")
    public Response<Animal> getAnimal(@PathVariable Long id) {
        return Response.success(animalService.getAnimal(id));
    }

    @Operation(summary = "获取动物列表")
    @GetMapping
    public Response<Page<Animal>> getAnimals(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Long categoryId,
            Pageable pageable) {
        return Response.success(animalService.getAnimals(name, categoryId, pageable));
    }

    @Operation(summary = "获取附近的动物")
    @GetMapping("/nearby")
    public Response<List<Animal>> getNearbyAnimals(
            @RequestParam Double latitude,
            @RequestParam Double longitude,
            @RequestParam(defaultValue = "1000") Double distance) {
        return Response.success(animalService.getNearbyAnimals(latitude, longitude, distance));
    }
} 